//
//  CommitDiffViewController.swift
//  GitOSC-Swift
//
//  Created by 李萍 on 16/3/11.
//  Copyright © 2016年 李萍. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class CommitDiffViewController: UITableViewController {
    var commit: GTCommit!
    var nameSpace: String!
    var commitDiffCell: CommitDiffCell!
    var commitDiffs: [GTDiff] = []
    var projectID: Int!
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.edgesForExtendedLayout = UIRectEdge.None
//        self.tableView.backgroundColor = UIColor.lightGrayColor()
//        self.tableView.tableFooterView = UIView.init(frame: CGRect.zero)
        
        self.tableView.registerNib(UINib(nibName: "CommitDiffCell", bundle: nil), forCellReuseIdentifier: "commitdiffCell")
        commitDiffCell = self.tableView.dequeueReusableCellWithIdentifier("commitdiffCell") as! CommitDiffCell
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "评论", style: UIBarButtonItemStyle.Plain, target: self, action: "commentList")
        
        self.fetchForCommitDiff()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

   //列表
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 2
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        } else {
            if commitDiffs.count > 0 {
                return commitDiffs.count
            }
            return 0
        }
    }

    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 0 {
            commitDiffCell!.contentLabel.text = commit.title
            let height = commitDiffCell.contentLabel.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
            
            return 75 + height
        } else {
            return 50
        }
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier("commitdiffCell", forIndexPath: indexPath) as! CommitDiffCell
            cell.setContentForCommitDiff(self.commit)
            return cell
        } else {
            let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
            
            if commitDiffs.count > 0 {
                let diff = commitDiffs[indexPath.row]
                let newPaths = diff.newPath.componentsSeparatedByString("/")
                let path = newPaths[newPaths.count-1]
                
                cell.textLabel?.text = path
                cell.detailTextLabel?.text = diff.oldPath

            }
            return cell
        }
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
            return 30
        }
        return 0
    }
    
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 1 {
            if commitDiffs.count > 0 {
                return "\(commitDiffs.count)个文件发生了变化"
            }
            
        }
        return ""
    }

    //获取数据
    func fetchForCommitDiff() {
        let userinfos = UserInfos()
        let private_token = userinfos.getprivateToken()
        
        let strUrl = "https://git.oschina.net/api/v3/projects/\(nameSpace)/repository/commits/\(commit.commitId)/diff?private_token=\(private_token)"
        
        Alamofire.Manager.sharedInstance.request(Method.GET, strUrl).responseJSON { response in
            if let results = response.result.value {
                let jsonArray = JSON(results).array!
                
                for i in 0 ..< jsonArray.count {
                    
                    let commitDiff = GTDiff(json: jsonArray[i])
                    self.commitDiffs.append(commitDiff)
                }
                
                self.tableView.reloadData()
            }
        }
    }
    
    //评论列表
    func commentList() {
        let commentVC = CommentViewController()
        commentVC.nameSpace = self.nameSpace
        commentVC.projectID = self.projectID
        commentVC.commitID = commit.commitId
        self.navigationController?.pushViewController(commentVC, animated: true)
    }
}
